export function closest (source, target) {
  if (!source || !target) {
    return null
  }
  let element = source
  while (element && element.nodeType === 1) {
    if (typeof target === 'string') {
      // 先只支持target为className的场景
      if (element.classList.contains(target)) {
        return element
      }
    } else {
      if (element === target) {
        return element
      }
    }
    element = element.parentNode
  }
  return null
}

function isElement (node) {
  const ELEMENT_NODE_TYPE = 1
  return node.tagName !== 'HTML' && node.tagName !== 'BODY' && node.nodeType === ELEMENT_NODE_TYPE
}

export function getScrollParent (el, root = window) {
  let node = el
  while (node && node !== root && isElement(node)) {
    const { overflowY } = window.getComputedStyle(node)
    if (/scroll|auto/i.test(overflowY)) {
      return node
    }
    node = node.parentNode
  }
  return root
}
